gulp = require 'gulp'
$ = require('gulp-load-plugins')()
browserSync = require 'browser-sync'
reload = browserSync.reload
args = require('yargs').argv
del = require 'del'

gHtml = (name='example',styles=[],scripts=[],tmpl='blank',dest='./app/html')->
	data = 
		name : name
		styles : styles
		scripts : scripts
	gulp.src "./app/templates/#{tmpl}.jade"
	.pipe $.data(data)
	.pipe $.jade({pretty : true})
	.pipe $.rename {
		basename : name
		prefix : "#{tmpl}."
	}
	.pipe gulp.dest(dest)
	# .pipe browserSync.reload({stream:true})

gCSS = (name='example',tmpl="blank",dest='./app/lib/styles')->
	gulp.src "./app/templates/#{tmpl}.css"
	.pipe $.rename {
		basename : name
	}
	.pipe gulp.dest(dest)

gJS = (name='example',tmpl="blank",dest='./app/lib/scripts')->
	gulp.src "./app/templates/#{tmpl}.js"
	.pipe $.rename {
		basename : name
	}
	.pipe gulp.dest(dest)

gulp.task 'serve' , ->
	browserSync {
		server : 
			baseDir : './app'
			directory : true    
			index: "index.html"
	}

gulp.task 'plugin' , ->
	name = args.name
	gHtml name , [name] , [name] , 'plugin'
	gCSS  name , null , "./app/lib/plugins/#{name}"
	gJS   name , null , "./app/lib/plugins/#{name}"

gulp.task 'ui' , ->
	name = args.name
	gHtml name , [name] , null , 'ui'
	gCSS name , null , "./app/lib/styles/"

gulp.task 'control' , ->
	name = args.name
	gHtml name , [] , [name] , 'control'
	gJS name , null , "./app/lib/scripts/"


gulp.task 'clear' , ->
	del ['./app/html/**/*.html','./app/lib/styles/**.{css,scss}','./app/lib/scripts/**/*.{js,coffee}','./app/lib/plugins/**']

gulp.task 'clear:plugin' , ->
	name = args.name || '*'
	del ["./app/html/plugin.#{name}.html","./app/lib/plugins/#{name}"]

gulp.task 'clear:ui' , ->
	name = args.name || '*'
	del ["./app/html/ui.#{name}.html","./app/lib/styles/#{name}.*"]

gulp.task 'clear:control' , ->
	name = args.name || '*'
	del ["./app/html/control.#{name}.html","./app/lib/scripts/#{name}.*"]


gulp.task 'css' , ->
	gulp.src './app/**/*.css'
	.pipe reload({stream : true})

gulp.task 'reload' , ->
	 browserSync.reload()

gulp.task 'default' ,['serve'] ,->
	gulp.watch './app/**/*.{js,html}' , ['reload']
	# console.log watcher
	gulp.watch './app/**/*.css' , ['css']
	# gulp.watch './app/**/*.js' , ['reload']